// pages/designer/detail/index.js
const router = require('../../../utils/router.js')
const api = require('../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isScan: false,
    imageHost: api.IMG_HOST,
    scrollTop: 0,
    designerID: null,
    currentTab: 'product',
    galleryList: [],
    desginer: {}
  },
  tapNavigateToUrl({ currentTarget }) {
    router.push(currentTarget.dataset.url)
  },
  handleTabChange({ detail }) {
    this.setData({
      currentTab: detail.key
    })
  },
  makePhoneCall({ currentTarget }) {
    wx.makePhoneCall({
      phoneNumber: currentTarget.dataset.tel
    })
  },
  /**
   * 获取设计师的图库
   */
  getGalleryListByDesigner() {
    return new Promise((resolve, reject) => {
      api._get('', {
        managerID: this.data.designerID,
        index: 1
      },
        {
          loading: false
        }).then(res => {
          if (res.Code === 100000) {
            this.setData({
              galleryList: res.Data
            })
            resolve(res.Data)
          }
        })
    })
  },
  /**
   * 获取设计师信息
   */
  getDesignerInfo() {
    return new Promise((resolve, reject) => {
      api._get('/User/Construction/Manager/Info', {
        managerID: this.data.designerID
      },
        {
          loading: false
        }).then(res => {
          if (res.Code === 100000) {
            this.setData({
              desginer: res.Data
            })
            resolve(res.Data)
          }
        })
    })
  },
  init() {
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    Promise.all([
      this.getDesignerInfo()
    ]).then(() => {
      wx.hideLoading()
    })
  },
  /**
   * 通过小程序码的scene获取设计师ID
   */
  getDesignerIDFromScene(scene) {
    return new Promise((resolve, reject) => {
      api._get('/User/Account/Designer/QRCode/Info', {
        scene: scene
      }).then(res => {
        if (res.Code === 100000) {
          resolve(res.Data)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      this.setData({
        isScan: true
      })
      this.getDesignerIDFromScene(options.scene).then(data => {
        this.setData({
          designerID: data
        })
        this.init()
      })
    } else {
      this.setData({
        designerID: options.id
      })
      this.init()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onPageScroll(option) {
    this.setData({
      scrollTop: option.scrollTop
    })
  }
})